<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $uses = array('User', 'Mailaddress', 'Transact');
	var $scaffold;
	var $protectedActions = array('index', 'add', 'edit', 'delete', 'view');
	var $helpers = array('Html', 'Form');
	var $components = array('RequestHandler', 'AdminAuth', 'Pmaimai', 'Token');
	
	
	function login()
	{
		$this->Session->delete($this->sessionUserName);
		$this->set('error', false);
		if(empty($this->data['User'])){
			return;
		}
		
		if(empty($this->data['User']['mailaddress'])){
			$this->_myErrorMsg[] = 'メールアドレスが未入力です';
		}
		if(empty($this->data['User']['password'])){
			$this->_myErrorMsg[] = 'パスワードが未入力です';
		}
		
		if(count($this->_myErrorMsg) !== 0){
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$mail = $this->Mailaddress->findByMailaddressAndStatus($this->data['User']['mailaddress'], 'active');
		if(!$mail){
			$this->_myErrorMsg[] = 'そんなユーザーいません';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->User->set($mail);
		if(!$this->User->isAuth($this->data['User']['password'])){
			$this->_myErrorMsg[] = 'そんなユーザーいません';
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Session->write($this->sessionUserName, $mail['User']['id']);
		$this->redirect('/');
	}
	
	function register()
	{
		$this->set('error', false);
		if(!$this->data){
			return;
		}
		
		
		if(empty($this->data['User']['read']) || $this->data['User']['read'] !== '1'){
			$this->User->invalidate('needagree', '登録には会員規約の同意が必要です');
		}
		if($this->data['User']['mailaddress'] !== $this->data['User']['confirmmail']){
			$this->User->invalidate('confirmmail', '入力と再入力のメールアドレスが違います');
		}
		if(empty($this->data['User']['mailaddress']) || empty($this->data['User']['confirmmail'])){
			$this->User->invalidate('existempty', 'メールアドレス未入力の箇所があります');
		}
		
		//トランザクション始まり
		$this->Transact->begin();
		
		$user = $this->User->register();
		if(!$user){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->User->validationErrors);
			return;
		}
		
		$userID = $this->User->getLastInsertID();
		$this->set('user', $user);
		
		if(!$this->Mailaddress->register($userID, $this->data['User']['mailaddress'])){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->Mailaddress->validationErrors);
			return;
		}
		
		$this->Mailaddress->unbindFully();
		$mail = $this->Mailaddress->read();
		$this->set('mail', $mail);
		
		$sendData = array();
		$sendData[] = array(
			'mailaddress' => $mail['Mailaddress']['mailaddress'],
			'viewpath' => '/email/user_register',
			'subject' => '【pmaimai】本登録のお願い',
		);
		
		if(!$this->_sendMail($sendData)){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->commit();
		$this->flash("仮登録メールが送信されました", '/');
	}
	
	function activate()
	{
		$this->Session->delete($this->sessionUserName);
		$this->set('error', false);
		//GETのデータはPOSTに入れちゃう　入力エラー再表示時対策
		if(empty($this->data['Mailaddress'])){
			$this->data['Mailaddress'] = array(
				'id' => $this->params['pass'][0],
				'token' => $this->params['pass'][1],
			);
		}
		
		//仮パスワードの照合ここから
		if(empty($this->data['Mailaddress']['id']) || empty($this->data['Mailaddress']['token'])){
			$this->redirect('/');
		}
		
		$this->Mailaddress->unbindFully();
		$mail = $this->Mailaddress->read(null, $this->data['Mailaddress']['id']);
		if(!$mail){
			$this->redirect('/');
		}
		$this->set('mail', $mail);
		
		$this->User->unbindFully();
		$user = $this->User->read(null, $mail['Mailaddress']['user_id']);
		$this->set('user', $user);
		
		if(!$this->User->checkActivate()){
			$this->User->invalidate('notactivatable', 'このユーザはアクティベート対象ではありません');
		}
		
		$this->Mailaddress->checkActivate($this->data['Mailaddress']['token'], $this->name);
		
		if(count($this->User->validationErrors) !== 0 || count($this->Mailaddress->validationErrors) !== 0){
			$errorMsg = array_merge($this->User->validationErrors, $this->Mailaddress->validationErrors);
			$this->flash(current($errorMsg), '/');
			return;
		}
		//仮パスワードの照合ここまで
		
		if(empty($this->data['User'])){
			return;
		}
		
		if($this->data['User']['password'] !== $this->data['User']['confirmpw']){
			$this->User->invalidate('confirm', '入力と再入力のパスワードが違います');
		}
		if(empty($this->data['User']['nickname']) || empty($this->data['User']['password']) || empty($this->data['User']['confirmpw'])){
			$this->User->invalidate('existempty', '未入力の箇所があります');
		}
		
		$this->Transact->begin();
		
		//ユーザをアクティベート
		if(!$this->User->activate($this->data['User']['nickname'], $this->data['User']['password'])){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->User->validationErrors);
			return;
		}
		$this->set('nickname', $this->data['User']['nickname']);
		
		//メールをアクティベート
		if(!$this->Mailaddress->updateStatus('activate')){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->Mailaddress->validationErrors);
			return;
		}
		
		$sendData = array();
		$sendData[] = array(
			'mailaddress' => $mail['Mailaddress']['mailaddress'],
			'viewpath' => '/email/user_activate',
			'subject' => '【pmaimai】本登録情報',
		);
		
		if(!$this->_sendMail($sendData)){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->_myErrorMsg);
			return;
		}
		
		$this->Transact->commit();
		$this->Session->write($this->sessionUserName, $this->User->getID());
		$this->flash('アクティベート完了しました','/');
		
	}
	

}
?>